import redis
from app_config import config

class RedisClient(object):
	def __init__(self):
		redis_config = config.get_redis_info()
		pool = redis.ConnectionPool(host=redis_config["host"], port=redis_config["port"], db=redis_config["db"])
		self.r = redis.Redis(connection_pool=pool)

	def set_key(self, key, value):
		self.r.set(key, value)

	def get_key(self, key):
		return str(self.r.get(key), encoding="utf-8")

	def set_hash_key(self, key, data):
		for k, v in data.items():
			if data[k] is None:
				data[k] = ""
		self.r.hmset(key, data)

	def get_hash_key(self, key):
		h = self.r.hgetall(key)
		res = {str(x, encoding='utf-8'):str(y, encoding='utf-8') for x, y in h.items()}
		return res

redis_client = RedisClient()
